package com.kefu.chat.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.basic.mapper.BasicStatusConfigMapper;
import com.kefu.basic.model.BasicStatusConfig;
import com.kefu.call.model.CallAgentAuxStatus;
import com.kefu.chat.vo.ChatStatusConfigInsertVo;
import com.kefu.chat.vo.ChatStatusConfigUpdateVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;
import com.kefu.common.util.Assert;
import com.kefu.common.util.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ChatConfigStatusService extends AbstractKefuService<BasicStatusConfig> {
    @Resource
    private BasicStatusConfigMapper basicStatusConfigMapper;

    public String addStatusConfig(ChatStatusConfigInsertVo chatStatusConfigVo) {
        Instant now = Instant.now();
        String tid = KefuContext.getTid();
        Assert.isTrue(basicStatusConfigMapper.selectCount(Wrappers.lambdaQuery(BasicStatusConfig.class)
                        .eq(BasicStatusConfig::getStatusName, chatStatusConfigVo.getStatusName())
                        .eq(BasicStatusConfig::getTenantId, tid)
                        .eq(BasicStatusConfig::getStatusType, 1)) > 0,
                "状态已经存在！");
        BasicStatusConfig info = chatStatusConfigVo.convert();
        Integer maxCode = basicStatusConfigMapper.selectMaxCode(tid, 1);
        Integer statusCode = maxCode == null ? 1 : maxCode + 1;
        Assert.isTrue(statusCode > 9, "在线状态数量已经到最大值");
        info.setStatusCode(String.valueOf(statusCode));
        basicStatusConfigMapper.insert(info);
        logger.info("chat_basicStatus_Add:{},{}ms", info, Duration.between(now, Instant.now()).toMillis());
        return info.getId();
    }

    public void deleteConfigStatus(String id) {
        BasicStatusConfig info = findById(id);
        Assert.isTrue(Objects.isNull(info),"已删除!");
        Assert.isTrue(1 == info.getIsDefault(), "系统默认状态不能删除!");
        Assert.isTrue(1 == info.getStatus(), "启动中不能删除！");
        deleteById(id);
        logger.info("删除在线自定义状态:{}", JacksonUtil.toString(info));
    }

    public String updateStatusConfig(ChatStatusConfigUpdateVo update) {
        String id = update.getId();
        BasicStatusConfig statusConfig = findById(id);
        Assert.isTrue(Objects.isNull(statusConfig), "状态已被删除！");
        if (1 == statusConfig.getIsDefault()) {
            Assert.isTrue(!update.getStatusName().equals(statusConfig.getStatusName()),
                    "不能修改默认状态的名称！");
            Assert.isTrue(Objects.nonNull(update.getStatus())
                    && 0 == update.getStatus(), "不能关闭默认的状态！");
        }
        LambdaQueryWrapper<BasicStatusConfig> wrapper = Wrappers.lambdaQuery(BasicStatusConfig.class);
        wrapper.eq(BasicStatusConfig::getStatusName, update.getStatusName());
        wrapper.eq(BasicStatusConfig::getTenantId, KefuContext.getTid());
        wrapper.eq(BasicStatusConfig::getStatusType, 1);
        List<BasicStatusConfig> list = list(wrapper);
        boolean flag = true;
        if (CollUtil.isNotEmpty(list)) {
            for (BasicStatusConfig config : list) {
                if (!config.getId().equals(update.getId())) {
                    flag = false;
                    break;
                }
            }
        }
        Assert.isTrue(!flag, "状态["+ update.getStatusName() +"]已经存在！");
        BasicStatusConfig info = update.convert();
        update(info);
        logger.info("chat_basicStatus_Update : {}, {}", update, JacksonUtil.toString(info));
        return info.getId();
    }
}
